package com.zxb.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.zxb.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zxb.system.domain.Beehivedata;
import com.zxb.system.mapper.BeehivesMapper;
import com.zxb.system.domain.Beehives;
import com.zxb.system.service.IBeehivesService;

/**
 * 蜂箱Service业务层处理
 * 
 * @author YY
 * @date 2024-10-14
 */
@Service
public class BeehivesServiceImpl implements IBeehivesService 
{
    @Autowired
    private BeehivesMapper beehivesMapper;

    /**
     * 查询蜂箱
     * 
     * @param beehiveId 蜂箱主键
     * @return 蜂箱
     */
    @Override
    public Beehives selectBeehivesByBeehiveId(Long beehiveId)
    {
        return beehivesMapper.selectBeehivesByBeehiveId(beehiveId);
    }

    /**
     * 查询蜂箱列表
     * 
     * @param beehives 蜂箱
     * @return 蜂箱
     */
    @Override
    public List<Beehives> selectBeehivesList(Beehives beehives)
    {
        return beehivesMapper.selectBeehivesList(beehives);
    }

    /**
     * 新增蜂箱
     * 
     * @param beehives 蜂箱
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBeehives(Beehives beehives)
    {
        int rows = beehivesMapper.insertBeehives(beehives);
        insertBeehivedata(beehives);
        return rows;
    }

    /**
     * 修改蜂箱
     * 
     * @param beehives 蜂箱
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBeehives(Beehives beehives)
    {
        beehivesMapper.deleteBeehivedataByBeehiveId(beehives.getBeehiveId());
        insertBeehivedata(beehives);
        return beehivesMapper.updateBeehives(beehives);
    }

    /**
     * 批量删除蜂箱
     * 
     * @param beehiveIds 需要删除的蜂箱主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBeehivesByBeehiveIds(Long[] beehiveIds)
    {
        beehivesMapper.deleteBeehivedataByBeehiveIds(beehiveIds);
        return beehivesMapper.deleteBeehivesByBeehiveIds(beehiveIds);
    }

    /**
     * 删除蜂箱信息
     * 
     * @param beehiveId 蜂箱主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBeehivesByBeehiveId(Long beehiveId)
    {
        beehivesMapper.deleteBeehivedataByBeehiveId(beehiveId);
        return beehivesMapper.deleteBeehivesByBeehiveId(beehiveId);
    }

    /**
     * 新增蜂箱数据信息
     * 
     * @param beehives 蜂箱对象
     */
    public void insertBeehivedata(Beehives beehives)
    {
        List<Beehivedata> beehivedataList = beehives.getBeehivedataList();
        Long beehiveId = beehives.getBeehiveId();
        if (StringUtils.isNotNull(beehivedataList))
        {
            List<Beehivedata> list = new ArrayList<Beehivedata>();
            for (Beehivedata beehivedata : beehivedataList)
            {
                beehivedata.setBeehiveId(beehiveId);
                list.add(beehivedata);
            }
            if (list.size() > 0)
            {
                beehivesMapper.batchBeehivedata(list);
            }
        }
    }
}
